#include "GameStateMachine.h"

void GameStateMachine::pushState(GameState* State)
{
	mGameStates.push_back(State);
	mGameStates.back()->OnEnter();
}

void GameStateMachine::popState()
{
	if(!mGameStates.empty())
	{
		if(mGameStates.back()->OnExit())
		{
			delete mGameStates.back();
			mGameStates.pop_back();
		}
	}
}

void GameStateMachine::changeState(GameState* State)
{
	if(!mGameStates.empty())
	{
		if(mGameStates.back()->getStateID() == State->getStateID())
		{
			return; //Same State
		}

		if(mGameStates.back()->OnExit())
		{
			delete mGameStates.back();
			mGameStates.pop_back();
		}
	}

	mGameStates.push_back(State);
	mGameStates.back()->OnEnter();
}

void GameStateMachine::Update()
{
	if(!mGameStates.empty())
		mGameStates.back()->Update();
}

void GameStateMachine::Draw()
{
	if(!mGameStates.empty())
		mGameStates.back()->Draw();
}
